<?php
namespace App\Models\Account;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use App\Admin\Extra\Search\Html;

class BehaviorSingle extends  Model{

	protected $table='behavior_log';
    protected $_db_select = 'remote_bgsi';

    public $_max_count = 0;
    

    public function __construct()
    {
        \App\Models\Area::RemoteBgsiSelectByArea(Request::get('area'));
    }

	public function paginate()
    {
    	$perPage = Request::get('per_page', 20);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        //$data = \DB::connection($this->selectdb())->table($tableAccount)->skip($page* $perPage)->take($perPage)->get();
        //$data = $data->toArray();
        $rw = $this->selectGroup();
        self::SourceCondition($rw);
        // $rw->groupBy("op_module", "op");
        $rw->groupBy("op_module");
        $total = $rw->count();
        $rw->orderBy('op_time', "DESC");
        $rw->skip($start)->take($perPage);
        $data =  $rw->get();
        $data = $data->toArray();

        \Log::debug(__METHOD__, [$rw->toSql()]);
        // $data = json_decode($data, true);
        \App\Models\Account\Behavior::attachItemWithOpAlias($data);
        
        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());
        // \Log::debug(__METHOD__, ['-----------------------']);
        return $paginator;
    }
    public function selectdb($select = ''){
        if ($select) {
            return $this->_db_select = $select;
        }
        return $this->_db_select;
    }
    public function selectGroup() {
        return \DB::connection($this->selectdb())->table($this->table)
        ->select('op','op_module', \DB::raw('count(id) as times'), \DB::raw('sum(op_val_1) as diamonds'));
    }
    public static function SourceCondition($rw) {
        $cond = Request::all();

        if ($cond['player_id']){
            $pids = explode(',', $cond['player_id']);
            $rw->whereIn("player_id", $pids);
        }
        if ($cond['op']){
            $rw->where("op", $cond['op']);
        }
        if ($cond['ops']) {
            $rw->whereIn("op", $cond['ops']);
        }
        // if ($cond['area']){
        //     $rw->where("area", $cond['area']);
        // }
        if (empty($cond['op_time'])) {
            $cond['op_time'] = ['start' => time() - 30 * 86400];
        }
        if (is_array($cond['op_time'])) {
            $start = \App\ModelStatistic\StatisticCommon::GetIntTime($cond['op_time']['start']) ?: 0;
            $rw->where("op_time", ">", $start);
            $end = \App\ModelStatistic\StatisticCommon::GetIntTime($cond['op_time']['end']) ?: 0;
            if ($end) {
                $rw->where("op_time", "<", $end);
            }
        }
    }

    public static function getBarButtons($param_arr) {
        // 货币型汇总Group by
        $ops_coin = [
            'S.DIAMOND_REDUCE' => '钻石--',
            'S.DIAMOND_ADD' => '钻石++',
            'S.TICKET_REDUCE' => '红钻--',
            'S.TICKET_ADD' => '红钻++',
            'S.GOLD_REDUCE' => '金币--',
            'S.GOLD_ADD' => '金币++',
        ];
        $ops_items = [
            'S.ITEM_ACCEPT' => '物品获得',
            'S.ITEM_TOKEN' => '物品扣除',
        ];
        $button_arr = [];
        $param_arr['ops'] = [];
        $button_arr[] = Html::button( 'All', \App\Service\Common::build_url_query(url()->current(),  $param_arr), 'btn-goto btn-info  btn-large');
        $param_arr['ops'] = array_keys($ops_coin);
        $button_arr[] = Html::button( 'Coin', \App\Service\Common::build_url_query(url()->current(),  $param_arr), 'btn-goto btn-info  btn-large');
        $param_arr['ops'] = array_keys($ops_items);
        $button_arr[] = Html::button( 'Item', \App\Service\Common::build_url_query(url()->current(),  $param_arr), 'btn-goto btn-info btn-large');
        foreach ($ops_coin as $op =>$label) {
            $param_arr['ops'] = [$op];
            $button_arr[] = Html::button( $label, \App\Service\Common::build_url_query(url()->current(),  $param_arr), 'btn-goto btn-warning');
        }
        return $button_arr;
    }
}